import mongoose from "mongoose";

let isConnected = false;

export const connectToDB = async () => {
  mongoose.set("strictQuery", true);

  if (!process.env.MONGODB_URI) return console.log("MongoDB URI not set.");

  if (isConnected) return console.log("MongoDB is already Connect.");

  try {
    await mongoose.connect(process.env.MONGODB_URI);
    isConnected = true;
    console.log("MongoDB Connected.");
  } catch (e) {
    console.log("mongoDB connect failed", e);
  }
};
